/**
 * 智慧小区管理系统 - 单元管理API接口
 * @author Wu.Liang
 * @date 2024-12-21
 * @updated 2025-01-30 - 完善单元管理API接口，确保与后端Controller完全匹配
 */
import api from './request'
import type { ApiResponse } from './request'

// 单元相关类型定义
export interface UnitData {
  id: number
  unitName: string
  unitNo: string
  unitCode: string
  buildingId: number
  buildingName: string
  communityId: number
  communityName: string
  floorCount: number
  roomCount: number
  householdCount: number
  hasElevator: number
  elevatorCount: number
  doorAccessType: number
  accessCode: string
  area: number
  managerId: number
  managerName: string
  facilities: string
  remark: string
  status: number
  sortOrder: number
  version: number
  deleted: number
  createBy: number
  createTime: string
  updateBy: number
  updateTime: string
  floorNumber: number
}

export interface CreateUnitDTO {
  unitName: string
  unitNo: string
  unitCode: string
  buildingId: number
  communityId: number
  floorCount: number
  roomCount: number
  householdCount: number
  hasElevator: number
  elevatorCount: number
  doorAccessType: number
  accessCode: string
  area: number
  managerId: number
  managerName: string
  facilities: string
  remark: string
  status: number
  sortOrder: number
}

export interface UpdateUnitDTO {
  unitName?: string
  unitNo?: string
  unitCode?: string
  buildingId?: number
  communityId?: number
  floorCount?: number
  roomCount?: number
  householdCount?: number
  hasElevator?: number
  elevatorCount?: number
  doorAccessType?: number
  accessCode?: string
  area?: number
  managerId?: number
  managerName?: string
  facilities?: string
  remark?: string
  status?: number
  sortOrder?: number
}

export interface UnitQueryDTO {
  unitName?: string
  unitNo?: string
  unitCode?: string
  buildingId?: number
  communityId?: number
  status?: number
  current?: number
  size?: number
}

export interface UnitLayoutData {
  unitId: number
  unitName: string
  buildingName: string
  floors: UnitFloorData[]
}

export interface UnitFloorData {
  floorNumber: number
  households: UnitHouseholdData[]
}

export interface UnitHouseholdData {
  id: number
  roomNumber: string
  status: number
  area: number
  roomCount: number
  hallCount: number
}

// 单元API接口
export const unitApi = {
  /**
   * 获取单元列表
   * @author Wu.Liang
   */
  getUnitList: (params: UnitQueryDTO) => 
    api.get<ApiResponse<UnitData[]>>('/api/region/units', { params }),

  /**
   * 获取单元分页列表
   * @author Wu.Liang
   */
  getUnitPage: (params: UnitQueryDTO) => 
    api.get<ApiResponse<any>>('/api/region/units', { params }),

  /**
   * 获取单元详情
   * @author Wu.Liang
   */
  getUnitDetail: (id: number) => 
    api.get<ApiResponse<UnitData>>(`/api/region/units/${id}`),

  /**
   * 根据单元编码获取单元
   * @author Wu.Liang
   */
  getUnitByCode: (unitCode: string) => 
    api.get<ApiResponse<UnitData>>(`/api/region/units/code/${unitCode}`),

  /**
   * 根据楼栋获取单元列表
   * @author Wu.Liang
   */
  getUnitsByBuilding: (buildingId: number) => 
    api.get<ApiResponse<UnitData[]>>(`/api/region/units/building/${buildingId}`),

  /**
   * 根据小区ID批量查询单元
   * @author Wu.Liang
   */
  queryUnitsByCommunity: (params: {
    communityIds: string
    buildingIds?: string
    groupByBuilding?: boolean
  }) => 
    api.get<ApiResponse<any>>('/api/region/units/queryByCommunity', { params }),

  /**
   * 获取单元下的房户列表
   * @author Wu.Liang
   */
  getUnitHouseholds: (unitId: number) => 
    api.get<ApiResponse<any>>(`/api/region/units/${unitId}/households`),

  /**
   * 创建单元
   * @author Wu.Liang
   */
  createUnit: (data: CreateUnitDTO) => 
    api.post<ApiResponse<UnitData>>('/api/region/units', data),

  /**
   * 更新单元
   * @author Wu.Liang
   */
  updateUnit: (id: number, data: UpdateUnitDTO) => 
    api.put<ApiResponse<UnitData>>(`/api/region/units/${id}`, data),

  /**
   * 删除单元
   * @author Wu.Liang
   */
  deleteUnit: (id: number) => 
    api.delete<ApiResponse<void>>(`/api/region/units/${id}`),

  /**
   * 更新单元状态
   * @author Wu.Liang
   */
  updateUnitStatus: (unitId: number, status: number) => 
    api.put<ApiResponse<boolean>>(`/api/region/units/${unitId}/status`, null, { 
      params: { status } 
    }),

  /**
   * 批量更新单元状态
   * @author Wu.Liang
   */
  batchUpdateUnitStatus: (unitIds: number[], status: number) => 
    api.put<ApiResponse<boolean>>('/api/region/units/batch/status', unitIds, { 
      params: { status } 
    }),

  /**
   * 批量删除单元
   * @author Wu.Liang
   */
  batchDeleteUnits: (unitIds: number[]) => 
    api.delete<ApiResponse<void>>('/api/region/units/batch', { data: unitIds }),

  /**
   * 检查单元编码唯一性
   * @author Wu.Liang
   */
  checkUnitCodeUnique: (unitCode: string, excludeUnitId?: number) => 
    api.get<ApiResponse<boolean>>('/api/region/units/check/code', { 
      params: { unitCode, excludeUnitId } 
    }),

  /**
   * 获取单元统计信息
   * @author Wu.Liang
   */
  getUnitStats: (unitId: number) => 
    api.get<ApiResponse<any>>(`/api/region/units/${unitId}/stats`),

  /**
   * 清除单元缓存
   * @author Wu.Liang
   */
  clearUnitCache: (unitId: number) => 
    api.delete<ApiResponse<boolean>>(`/api/region/units/cache/${unitId}`),

  /**
   * 清除所有单元缓存
   * @author Wu.Liang
   */
  clearAllUnitCache: () => 
    api.delete<ApiResponse<boolean>>('/api/region/units/cache/all'),

  /**
   * 获取单元布局
   * @author Wu.Liang
   */
  getUnitLayout: (id: number) => 
    api.get<ApiResponse<UnitLayoutData>>(`/api/region/units/${id}/layout`),

  /**
   * 批量生成单元
   * @author Wu.Liang
   */
  batchGenerate: (data: any) => 
    api.post<ApiResponse<void>>('/api/region/units/batch-generate', data),

  /**
   * 获取单元统计信息
   * @author Wu.Liang
   */
  getUnitStatistics: (id: number) => 
    api.get<ApiResponse<any>>(`/api/region/units/${id}/statistics`)
} 